package cn.tao.user.service.impl;


import cn.tao.base.exception.BusinessException;
import cn.tao.base.service.impl.BaseServiceImpl;
import cn.tao.user.domain.LoginInfo;
import cn.tao.user.domain.dto.UserDto;
import cn.tao.user.mapper.LoginInfoMapper;
import cn.tao.user.service.ILoginInfoService;
import cn.tao.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class LoginInfoServiceImpl extends BaseServiceImpl<LoginInfo> implements ILoginInfoService {

    @Autowired
    private LoginInfoMapper loginInfoMapper;

    @Override
    public LoginInfo login(UserDto userDto) throws BusinessException {
        //根据手机号拿到注册用户信息
        LoginInfo loginInfo = loginInfoMapper.loadByAccount(userDto.getPhone());
        //1.比较账户是否正确
        if (loginInfo==null){
            throw new BusinessException("用户名或密码错误");
        }
        //2.比较密码是否正确,把传递进来的密码+查询到的用户的盐进行加密，然后与查询到用户的密码进行比较
        String byMd5 = MD5Utils.encrypByMd5(userDto.getPassword() + loginInfo.getSalt());
        if (!loginInfo.getPassword().equals(byMd5)){
            throw new BusinessException("用户名或密码错误");
        }
        return loginInfo;

    }
}
